/// 页面 * 登录
<template>
  <h2>Login</h2>
  <hr />
  <ExForm v-bind="form.bind">
    <ExFormItem label="用户名" name="userName" :rule="rule()">
      <AInput />
    </ExFormItem>
    <ExFormItem label="密码" name="password" :rule="rule()">
      <AInputPassword />
    </ExFormItem>
  </ExForm>
  <hr />
  <AButton type="primary" :loading="loading.value" @click="login"> login </AButton>
</template>

<script setup lang="ts">
import { rule } from '@expresso/plus'
import { type LoginParams, apiLogin } from '@/apis/Login'
import { cache, isSucceed } from '@/helpers'

const loading = useLoading('local')

const form = useForm<LoginParams>()

const router = useRouter()

const login = async () => {
  try {
    const data = await form.api!.validateFields()
    // router.push('/')
    loading(async () => {
      const { data: res, headers } = await apiLogin(data)
      if (isSucceed(res)) {
        const token = headers.token
        cache.setToken(token)
        router.push('/')
      }
    })
  } catch {}
}
</script>

<style scoped></style>
